<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/4/19
 * Time: 22:10
 */

namespace app\api\model;


use think\Model;

class Address extends  Model
{
    protected $autoWriteTimestamp = true;

    protected $createTime='create_time';
    protected $dateFormat='Y年m月d日';
    //开启自动设置
    protected $auto = [];
    protected $insert = ['create_tiame'];


    public function select(){
        $res = self::all();
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }

    public function add($data)
    {
        // if(!empty($data['default']) && $data['default']==1) {
        //     $default = self::where('uid', $data['uid'])->where('default', 1)->setField('default',0);
        // }
        $address = self::save($data);
        if($address){
            return_json(200,'添加地址成功');
        }else{
            return_json(400,'添加地址失败');
        }

    }

    public function del($id){
        $res = self::destroy($id);
        if($res){
            return_json(200,'删除成功');
        }else{
            return_json(400,'删除失败');
        }
    }

    public function detail($id){

        $res = $this->where('id',$id)->find();
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }

    public function modify($data)
    {
        if(!empty($data['default']) && $data['default']==1) {
            $default = self::where('uid', $data['uid'])->where('default', 1)->setField('default',0);
        }
            $res = self::where('id', $data['id'])->update($data);
            if ($res) {
                return_json(200, '修改成功');
            } else {
                return_json(400, '修改失败');
            }
        }


    public function det($uid){
        $res = self::where('uid',$uid)->select();
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }

}